package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.itheima.mapper.BrandMapper;
import com.itheima.pojo.Brand;
import com.itheima.pojo.BrandQueryParam;
import com.itheima.pojo.PageBean;
import com.itheima.service.BrandService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService {

    @Autowired
    private BrandMapper brandMapper;



    @Override
    public PageBean page(BrandQueryParam queryParam) {
        // //1. 设置请求参数
        // PageHelper.startPage(queryParam.getPage(), queryParam.getPageSize());
        //
        // //2. 执行查询
        // List<Brand> categoryList = brandMapper.list(queryParam);
        // Page<Brand> page = (Page<Brand>) categoryList;
        //
        // //3. 封装结果
        // return new PageBean(page.getTotal(), page.getResult());
        // 1、构建一个Page对象
        Page<Brand> page = new Page<>(queryParam.getPage(), queryParam.getPageSize());

        // 2、构建查询条件
        // LambdaQueryWrapper<Brand> queryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Brand> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.like(queryParam.getName() != null, Brand::getName, queryParam.getName());
        queryWrapper.like(queryParam.getFirstLetter() != null, Brand::getFirstLetter, queryParam.getFirstLetter());

        // 3、分页查询
        page = this.page(page, queryWrapper);

        PageBean pageBean = new PageBean(page.getTotal(), page.getRecords());

        return pageBean;

    }

}
